﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace VisualGTest
{
    /// <summary>
    /// Expected to format instances of TestFailureInfo.
    /// </summary>
    class FailureInfoCell : DataGridViewTextBoxCell
    {
        private DataGridViewTriState m_previousWrapMode;

        #region property backing variables
        private bool _showingMessage;
        #endregion

        #region properties

        /// <summary>
        /// Gets or sets whether the detailed failure message is displayed.
        /// </summary>
        public bool ShowingMessage
        {
            get
            {
                return _showingMessage;
            }
            set
            {
                if (_showingMessage != value)
                {
                    _showingMessage = value;

                    if (_showingMessage)
                    {
                        m_previousWrapMode = this.Style.WrapMode;

                        // enable multi-line text
                        this.Style.WrapMode = DataGridViewTriState.True;
                    }
                    else
                    {
                        // return to previous wrap mode
                        this.Style.WrapMode = m_previousWrapMode;
                    }
                }
            }
        }

        #endregion

        public FailureInfoCell()
        {
            _showingMessage = false;
            m_previousWrapMode = DataGridViewTriState.NotSet;
        }

        protected override Object GetFormattedValue(
            Object value,
            int rowIndex,
            ref DataGridViewCellStyle cellStyle,
            TypeConverter valueTypeConverter,
            TypeConverter formattedValueTypeConverter,
            DataGridViewDataErrorContexts context)
        {
            TestFailureInfo failureInfo = value as TestFailureInfo;

            if (failureInfo == null || failureInfo.LineNumber == -1)
            {
                return "";
            }

            string formattedValue = string.Format("[{0}, line {1}]: {2}",
                failureInfo.FileName, failureInfo.LineNumber, failureInfo.Summary);

            if (_showingMessage && failureInfo.Message.Length > 0)
            {
                formattedValue += "\n" + failureInfo.Message;
            }

            return formattedValue;
        }

        public override object Clone()
        {
            FailureInfoCell clone = base.Clone() as FailureInfoCell;

            clone.ShowingMessage = this.ShowingMessage;

            return clone;
        }
    }
}
